package com.itcast.flowable.controller;

import com.basic.flowable.entity.ResourceViewVo;
import com.basic.flowable.entity.Workflow;
import com.basic.flowable.enums.ResourceTypeEnum;
import com.basic.flowable.service.FlowBusinessService;
import com.basic.flowable.service.FlowEngineService;
import com.itcast.flowable.entity.ResponseModel;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作流测试接口
 *
 * @author zheng.zhang
 */
@RestController
@AllArgsConstructor
@RequestMapping("flowable")
public class FlowableController {
    private final FlowBusinessService flowBusinessService;
    private final FlowEngineService flowEngineService;

    /**
     * 获取用户任务
     *
     * @param userId 用户ID
     * @return 用户任务列表
     */
    @GetMapping("user-tasks")
    public ResponseModel<List<Workflow>> getUserTasks(String userId) {
        List<Workflow> workflows = flowBusinessService.selectDonePage(userId);
        return ResponseModel.data(workflows);
    }

    /**
     * 发起流程
     *
     * @return 流程对象
     */
    @GetMapping("start-process")
    public ResponseModel<String> startProcess(String userId, double money) {
        Map<String, Object> variables = new HashMap<>(8);
        variables.put("taskUser", userId);
        variables.put("money", money);
        Workflow workflow = flowEngineService.startProcess(Workflow.builder()
                .processDefinitionKey("Expense")
                .businessTable("ExpenseProcess")
                .variables(variables)
                .build(), userId);
        return ResponseModel.data(workflow.getProcessInstanceId());
    }

    /**
     * 审批任务
     *
     * @param taskId     任务ID
     * @param instanceId 流程实例ID
     * @param comment    评论
     * @param isPass     是否通过
     * @return 审批结果
     */
    @PostMapping("complete-process")
    public ResponseModel<Boolean> completeProcess(@RequestParam String taskId, @RequestParam String instanceId, String comment, boolean isPass) {
        Map<String, Object> variables = new HashMap<>(8);
        variables.put("employee", "manager1");
        boolean completeTask = flowBusinessService.completeTask(Workflow.builder()
                .taskId(taskId)
                .processInstanceId(instanceId)
                .comment(comment)
                .variables(variables)
                .isPass(isPass)
                .build());
        return ResponseModel.data(completeTask);
    }

    /**
     * 流程图预览
     *
     * @param instanceId 实例ID
     * @param response   响应结果
     */
    @GetMapping("resource-view")
    public void resourceView(String instanceId, HttpServletResponse response) {
        try {
            flowEngineService.resourceView(ResourceViewVo.builder()
                    .processInstanceId(instanceId)
                    .resourceType(ResourceTypeEnum.IMAGE)
                    .build(), response);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 节点流程图
     *
     * @param instanceId 实例ID
     * @param response   响应结果
     */
    @GetMapping("diagram")
    public void diagram(String instanceId, HttpServletResponse response) {
        flowEngineService.diagram(instanceId, response);
    }
}
